/**
 * 
 */
package uk.co.altv.simpledb;

import java.io.UnsupportedEncodingException;

import javax.crypto.spec.SecretKeySpec;

import uk.co.altv.simpledb.coder.HardBase64Coder;

/**
 * @author niki
 * 
 */
public class ConnectionFactory {

	public static final class Endpoint {
		public static final String US_EAST = "sdb.amazonaws.com";
		public static final String US_WEST_1 = "sdb.us-west-1.amazonaws.com";
		public static final String EU_WEST_1 = "sdb.eu-west-1.amazonaws.com";
	}

	private static boolean forceHTTPS = true;

	/**
	 * @return the forceHTTPS
	 */
	public static boolean isForceHTTPS() {
		return forceHTTPS;
	}

	/**
	 * @param forceHTTPS
	 *            the forceHTTPS to set
	 */
	public static void setForceHTTPS(boolean forceHTTPS) {
		ConnectionFactory.forceHTTPS = forceHTTPS;
	}

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param endpoint
	 * @return
	 * @throws InitialisationException 
	 */
	public static Connection getConnection(String accessKey, String secretKey,
			String endpoint) throws InitialisationException {
		try {
			Connection conn = new Connection(accessKey, new SecretKeySpec(
					secretKey.getBytes("utf-8"), "HmacSHA256"),
					isForceHTTPS() ? "https://" : "http://", endpoint);
			conn.setBase64Coder(new HardBase64Coder());
			return conn;
		} catch (UnsupportedEncodingException e) {
			throw new InitialisationException(e);
		}
	}
}
